package com.mvp.lt.airlineview.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.mvp.lt.airlineview.R;
import com.mvp.lt.airlineview.base.BaseActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.mvp.lt.airlineview.utils.LogUtil;
import com.mvp.lt.airlineview.utils.SysUtils;
import com.mvp.lt.airlineview.utils.ToastUtils;
import com.mvp.lt.airlineview.view.PhotoEditorView;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by LiuTao on 2019/11/27 0027 上午 9:14
 * functiona:
 */
public class PhotoEditorActivity extends BaseActivity {
    private static final String TAG = "PhotoEditorActivity";
    @BindView(R.id.selected_btn)
    Button selectedBtn;
    @BindView(R.id.save_btn)
    Button saveBtn;
    @BindView(R.id.show_path)
    TextView showPath;
    @BindView(R.id.photoView)
    PhotoEditorView mPhotoEditorView;
    @BindView(R.id.show_graffic_iv)
    ImageView showGrafficIv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_editor);
        ButterKnife.bind(this);
        String image_path = Environment.getExternalStorageDirectory().getPath() + "/quantum/" + "DJI_0083.jpg";
        LogUtil.e(TAG, image_path + "");
        mPhotoEditorView.setEditable(true);
        setOriginBitmap(image_path);

        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sdcard = Environment.getExternalStorageDirectory().getPath() + "/ACanVas/";
                String name = System.currentTimeMillis() + ".jpg";
                mPhotoEditorView.saveBitmapCanvas(sdcard + name,
                        new PhotoEditorView.SavePictureProgressListener() {
                            @Override
                            public void onSaveSuccess() {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        RequestOptions requestOptions = new RequestOptions()
                                                .fitCenter();
                                        Glide.with(PhotoEditorActivity.this)
                                                .load(sdcard + name)
                                                .apply(requestOptions)
                                                .into(showGrafficIv);
                                    }
                                });
                            }

                            @Override
                            public void onSaveFaild() {
                                ToastUtils.showToast("保存失败");
                            }
                        });
                showPath.setText(sdcard + name);

            }
        });
        selectedBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    /**
     * 设置原始图
     */
    public void setOriginBitmap(String path) {
        if (TextUtils.isEmpty(path)) {
            ToastUtils.showToast("图片路径不能为空");
            return;
        }
        File file = new File(path);
        if (file.exists()) {
            Bitmap bitmap = BitmapFactory.decodeFile(path);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mPhotoEditorView.getLayoutParams();
            int pw = SysUtils.getScreenWidth(this);
            if (bitmap != null) {
                int bw = bitmap.getWidth();
                int bh = bitmap.getHeight();
                params.height = (bh * pw) / bw;
            }
            params.width = pw;
            params.gravity = Gravity.CENTER;
            RequestOptions requestOptions = new RequestOptions()
                    .override(Target.SIZE_ORIGINAL)
                    .fitCenter();
            Glide.with(this)
                    .asBitmap()
                    .load(path)
                    .apply(requestOptions)
                    .into(mPhotoEditorView);
            mPhotoEditorView.requestLayout();
        } else {
            ToastUtils.showToast("图片不存在");
            finish();
        }
    }
}
